var app = angular.module('ZJPC')
app.directive('pagining', function () {
    return {
        restrict: 'AE',
        templateUrl: '../templates/directive/pagin.html',
        scope:{
            pageOption:'=pageoption',
            callBack:'&callback'
        },
        transclude:true,
        replace:true,
        link: pageLink
    }
});

app.directive('paginings', function () {
    return {
        restrict: 'AE',
        templateUrl: '../templates/directive/pagins.html',
        scope:{
            pageOption:'=pageoption',
            callBack:'&callback'
        },
        transclude:true,
        replace:true,
        link: pageLink
    }
});

function pageLink($scope, element, attrs) {
    $scope.$watch('pageOption', function (value,oldValue) {
        if(value.itemCount!=oldValue.itemCount||value.pageSize!=oldValue.pageSize){
            $scope.paginShow = false;
            var pageOption = value;
            pageOption['current']=value['current']==undefined?0:value['current'];
            var pageSize = pageOption.pageSize;
            var count = pageOption.itemCount;
            pageOption['pageCount'] = Math.ceil(count/pageSize);
            var pageCount =pageOption.pageCount;
            pageOption['pages']=[]
            if(pageCount !=0){
                for(var i=0;i<pageCount;i++){
                    if(i>Math.floor(count/pageSize)-1){
                        pageOption['pages'].push([i*pageSize+1,count])
                    }else{
                        pageOption['pages'].push([i*pageSize+1,(1+i)*pageSize])
                    }
                }
            }else{
                pageOption['pages']= [[1,0]]
            }
            $scope.pageOption = pageOption; 
            if(value.pageSize!=oldValue.pageSize){
                $scope.callBack({item:$scope.pageOption.pages[$scope.pageOption.current]});    
            }
             $scope.paginShow = true;
        }
/*                if(value!=oldValue){
            var pageOption = value;
            pageOption['current']=value['current']==undefined?0:value['current'];
            var pageSize = pageOption.pageSize;
            var count = pageOption.itemCount;
            pageOption['pageCount'] = Math.ceil(count/pageSize);
            var pageCount =pageOption.pageCount;
            pageOption['pages']=[]
            if(pageCount !=0){
                for(var i=0;i<pageCount;i++){
                    if(i>Math.floor(count/pageSize)-1){
                        pageOption['pages'].push([i*pageSize+1,count])
                    }else{
                        pageOption['pages'].push([i*pageSize+1,(1+i)*pageSize])
                    }
                }
            }else{
                pageOption['pages']= [[1,0]]
            }
            $scope.pageOption = pageOption; 
        }*/
/*            $scope.callBack({item:$scope.pageOption.pages[$scope.pageOption.current]});*/
    },true);
    $scope.clickPage = function(page,$event){
        if(!$(event.target).parent().hasClass('disabled')&&$scope.pageOption.current != page){ //判定是否失效
            $("#pageInput").val(page+1)
            $scope.pageOption.current = page;
            $scope.callBack({item:$scope.pageOption.pages[$scope.pageOption.current]});
        }
    }
    $scope.clickPageButton = function(){
        if($("#pageInput").val()<1||$("#pageInput").val()>$scope.pageOption.pageCount){
            alert("该页码不存在")
            $("#pageInput").val($scope.pageOption.current+1)
            return false;
        }else if($scope.pageOption.current != $("#pageInput").val()-1){
            $scope.pageOption.current=$("#pageInput").val()-1;
            $scope.callBack({item:$scope.pageOption.pages[$scope.pageOption.current]});
        }
    }
}


